package com.yt.sys.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.phone.models.dto.HistoryDTO;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.yt.sys.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yt.sys.mapper.UserMapper;
import com.yt.sys.models.service.IUserService;
import com.yt.sys.models.bo.UserSaveBO;
import com.yt.sys.models.dto.UserDTO;

import java.util.List;

/**
 * 客户信息控制器
 *
 * @author lizicong
 * @Date 2025-10-29 21:42:52
 */
@DubboService
public class UserServiceImpl extends ServiceImpl<UserMapper, UserDTO> implements IUserService{

    @Resource
    private YmlConfig ymlConfig;

    @Resource
    private UserMapper userMapper;

    @Override
    public boolean add(UserSaveBO saveBO) {
        UserDTO dto = new UserDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public List<UserDTO> getuserList() {
        List<UserDTO> userList = userMapper.selectList(null);
        return userList;
    }

    @Override
    public Page<UserDTO> getpageuserList(int currentPage) {
        // 1. 校验页码（最小为1）
        if (currentPage < 1) {
            currentPage = 1;
        }
        // 2. 定义每页条数
        int pageSize = 5;
        // 3. 计算分页起始位置
        int offset = (currentPage - 1) * pageSize;
        RowBounds rowBounds = new RowBounds(offset, pageSize);

        EntityWrapper<UserDTO> wrapper = new EntityWrapper<>();
        wrapper.orderBy("create_time", false);
        // 4. 获取当前页数据（List类型）
        List<UserDTO> records = userMapper.selectPage(rowBounds, wrapper);

        // 5. 查询总记录数
        int totalCount = userMapper.selectCount(null);

        // 6. 手动封装 Page 对象（无需设置 pages，由 Page 自动计算）
        Page<UserDTO> page = new Page<>(currentPage, pageSize);
        page.setRecords(records); // 设置当前页数据
        page.setTotal(totalCount); // 设置总记录数（pages 会自动根据 total 和 size 计算）

        return page;
    }

    @Override
    public List<UserDTO> searchgetuserList(String keyword) {
        Wrapper<UserDTO> wrapper = new EntityWrapper<>();
        wrapper.like("user_name", keyword)
                .or()
                .like("user_address", keyword);
        List<UserDTO> userList = userMapper.selectList(wrapper);
        return userList;
    }
    @Override
    public List<UserDTO> searchgetpublicuserList(String keyword) {
        Wrapper<UserDTO> wrapper = new EntityWrapper<>();
        wrapper.like("user_name", keyword)
                .or()
                .like("user_address", keyword)
                .and()
                .eq("user_type", "公客")
                .and()
                .orderBy("create_time", false);
        List<UserDTO> userList = userMapper.selectList(wrapper);
        return userList;
    }
    @Override
    public List<UserDTO> searchgetbusinessuserList(String keyword) {
        Wrapper<UserDTO> wrapper = new EntityWrapper<>();
        wrapper.like("user_name", keyword)
                .or()
                .like("user_address", keyword)
                .and()
                .eq("user_type", "商客")
                .and()
                .orderBy("create_time", false);
        List<UserDTO> userList = userMapper.selectList(wrapper);
        return userList;
    }
    @Override
    public Page<UserDTO> getpagepublicuserList(int currentPage) {
        // 1. 创建公客筛选条件
        Wrapper<UserDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_type", "公客"); // 筛选用户类型为公客

        // 2. 校验页码（最小为1）
        if (currentPage < 1) {
            currentPage = 1;
        }

        // 3. 定义分页参数
        int pageSize = 5;
        int offset = (currentPage - 1) * pageSize;
        RowBounds rowBounds = new RowBounds(offset, pageSize);

        EntityWrapper<UserDTO> wrapper1 = new EntityWrapper<>();
        wrapper1.orderBy("create_time", false)
                .and()
                .eq("user_type", "公客");
        // 4. 获取当前页数据（传入wrapper，应用筛选条件）
        List<UserDTO> records = userMapper.selectPage(rowBounds, wrapper1);

        // 5. 查询符合条件的总记录数（传入wrapper，统计公客总数）
        int totalCount = userMapper.selectCount(wrapper);

        // 6. 封装分页对象
        Page<UserDTO> page = new Page<>(currentPage, pageSize);
        page.setRecords(records); // 当前页公客数据
        page.setTotal(totalCount); // 公客总记录数

        return page;
    }
    @Override
    public Page<UserDTO> getpagebusinessuserList(int currentPage) {
        // 1. 创建商客筛选条件（注意这里之前写错了，应该是"商客"）
        Wrapper<UserDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_type", "商客"); // 筛选用户类型为商客

        // 2. 校验页码（最小为1）
        if (currentPage < 1) {
            currentPage = 1;
        }

        // 3. 定义分页参数
        int pageSize = 5;
        int offset = (currentPage - 1) * pageSize;
        RowBounds rowBounds = new RowBounds(offset, pageSize);

        EntityWrapper<UserDTO> wrapper2 = new EntityWrapper<>();
        wrapper2.orderBy("create_time", false)
                .and()
                .eq("user_type", "商客");
        // 4. 获取当前页数据（传入wrapper，应用筛选条件）
        List<UserDTO> records = userMapper.selectPage(rowBounds, wrapper2);

        // 5. 查询符合条件的总记录数（传入wrapper，统计商客总数）
        int totalCount = userMapper.selectCount(wrapper);

        // 6. 封装分页对象
        Page<UserDTO> page = new Page<>(currentPage, pageSize);
        page.setRecords(records); // 当前页商客数据
        page.setTotal(totalCount); // 商客总记录数

        return page;
    }
}
